#include "Stars.h"
#include <cmath>

#define PI 3.1415926535

Stars::Stars()
{
}

Stars::Stars(GLfloat radius , GLfloat distance , GLfloat speed , GLfloat selfSpeed , Stars * parent)
{
	this->radius = radius;
	this->selfSpeed = selfSpeed;
	this->alphaSelf = this->alpha = 0;
	this->distance = distance;

	for (int i = 0 , imax = 4; i < imax; i++) {
		this->rgbaColor[i] = 1.0f;
	}
	this->parentStar = parent;

	if (speed > 0)
		this->speed = 360.0f / speed;
	else
		this->speed = 0.0f;
}


Stars::~Stars()
{
}

void Stars::drawStar()
{
	glEnable(GL_LINE_SMOOTH);
	glEnable(GL_BLEND);

	int n = 1440;
	glPushMatrix();
	{
		if (parentStar != 0 && parentStar->distance > 0) {
			glRotatef(parentStar->alpha , 0 , 0 , 1);
			glTranslatef(parentStar->distance , 0.0 , 0.0);
		}
		glBegin(GL_LINES);
		for (int i = 0; i<n; ++i)
			glVertex2f(distance * cos(2 * PI * i / n) ,
				distance * sin(2 * PI * i / n));
		glEnd();
		glRotatef(alpha , 0 , 0 , 1);
		glTranslatef(distance , 0.0 , 0.0);

		glRotatef(alphaSelf , 0 , 0 , 1);

		glColor3f(rgbaColor[0] , rgbaColor[1] , rgbaColor[2]);
		glutSolidSphere(radius , 40 , 32);
	}
	glPopMatrix();
}

void Stars::update(long timeSpan)
{
	alpha += timeSpan * speed;
	alphaSelf += selfSpeed;
}
